<template>
  <el-dialog :title="!dataForm.id ? '菜单新增' : '菜单编辑'" :close-on-click-modal="false" :visible.sync="visible" width="45%">
    <el-alert title="提示" type="warning" show-icon class="clearfix" description="
              注意：一级菜单访问地址为名字简称。如：/auth/n
              二级菜单访问地址为代码配置的key。如：system_menu
              菜单的上级菜单只能是一级和二级，按钮的上级菜单只能是三级
            ">
    </el-alert>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="20%" style="width:80%; margin:auto;">
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio v-for="(type, index) in typeList" :label="index" :key="index" @change="radioChange(type)">{{ type }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="typeList[dataForm.type] + '名称'" prop="name">
        <el-input v-model="dataForm.name" :placeholder="typeList[dataForm.type] + '名称'"></el-input>
      </el-form-item>
      <el-form-item label="上级菜单" prop="parentName">
        <el-popover ref="menuListPopover" placement="bottom-start" trigger="click" style="height: 250px;min-width:300px!important;">
          <el-tree style="height: 230px;min-width:280px!important; overflow-y: scroll;overflow-x:visible" :data="menuList" :props="menuListTreeProps" node-key="id" ref="menuListTree" @current-change="menuListTreeCurrentChangeHandle" :default-expanded-keys="[0]" :highlight-current="true" :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:menuListPopover :readonly="true" placeholder="点击选择上级菜单" class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item v-if="dataForm.type !== 1" label="访问路径" prop="uri">
        <el-input v-model="dataForm.uri" placeholder="请输入菜单简称，如：auth"></el-input>
      </el-form-item>
      <el-form-item label="排序号" prop="orderNum">
        <el-input-number v-model="dataForm.displayOrder" :min="0" label="排序号" style="width:100%"></el-input-number>
      </el-form-item>
      <el-form-item v-if="dataForm.type !== 1" label="菜单图标" prop="icon">
        <el-row>
          <el-col :span="24">
            <el-popover ref="iconListPopover" placement="bottom-start" trigger="click" popper-class="mod-menu__icon-popover">
              <div class="mod-menu__icon-list" style="max-height: 250px;">
                <el-button v-for="(item, index) in iconList" :key="index" @click="iconActiveHandle(item)" :class="{ 'is-active': item === dataForm.icon }">
                  <svg-icon :icon-class="item"></svg-icon>
                </el-button>
              </div>
            </el-popover>
            <el-input v-model="dataForm.icon" v-popover:iconListPopover :readonly="true" placeholder="菜单图标名称" class="icon-list__input"></el-input>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" v-bind:loading="buttonLoading" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
import Icon from '@/icons'
import { getMenuList,addMenu,getMenuDetail,updateMenu} from '@/api/auth/resource'
export default {
  data() {
    var validateUrl = (rule, value, callback) => {
     if (!/\S/.test(value)) {
        callback(new Error('访问路径不能为空！'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      buttonLoading: false,
      parentLevel: 0,
      typeList: ['菜单', '按钮'],
      dataForm: {
        id: 0,
        name: '',
        type: 0,
        parentId: 0,
        parentName: '一级菜单',
        uri: '',
        displayOrder: 0,
        icon: '',
        status: 1
      },
      iconList: [],
      menuList: [],
      menuListTreeProps: {
        label: 'name',
        children: 'children'
      },
      dataRule: {
        name: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' },
          { max: 50, message: '最多输入 50 个字符', trigger: 'blur' }
        ],
        parentName: [
          { required: true, message: '上级菜单不能为空', trigger: 'change' },
          { max: 50, message: '最多输入 50 个字符', trigger: 'blur' }
        ],
        uri: [
          { required: true, validator: validateUrl, trigger: 'blur' },
          { max: 100, message: '最多输入 100 个字符', trigger: 'blur' }
        ],
        method: [
          { required: true, message: '访问方式不能为空', trigger: 'change' }
        ],
        code: [
          { required: true, message: '授权标识不能为空', trigger: 'change' }
        ],
        icon: [
          { required: true, message: '菜单图标不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.iconList = Icon.getNameList()
  },
  methods: {
    init(id) {
      this.dataForm.id = id;
      getMenuList().then((response) => {
        this.menuList = treeDataTranslate(response.data, 'id')
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (!this.dataForm.id) {
          // 新增
        } else {
          // 修改
          getMenuDetail(this.dataForm.id).then((response) => {
            var data = response.data
            this.dataForm.id = data.id;
            this.dataForm.type = data.type;
            this.dataForm.name = data.name;
            this.dataForm.parentId = data.parentId;
            this.dataForm.uri = data.uri;
            this.dataForm.displayOrder = data.displayOrder;
            this.dataForm.icon = data.icon;
            if(this.dataForm.parentId == 0){
              this.dataForm.parentName = '一级菜单'
            }else{
              this.menuListTreeSetCurrentNode();
            }
          })
        }
      })
    },
    //单选按钮change事件
    radioChange(type) {
      if (type == 0) {
        this.dataForm.uri = "";
        this.dataForm.icon = "";
        this.dataForm.parentId = 0;
        this.dataForm.parentName = '一级菜单'
      } else {
        this.dataForm.parentId = 0;
        this.dataForm.parentName = '一级菜单'
      }
    },
    // 菜单树选中
    menuListTreeCurrentChangeHandle(data, node) {
      if (this.dataForm.type == 0 && data._level > 2) {
        this.$notify({
          message: '当前菜单不能作为上级菜单,请更换！',
          type: 'warning',
          duration: 1500,
        })
        return;
      }
      if (this.dataForm.type == 1 && data._level != 3) {
        this.$notify({
          message: '当前菜单不能作为上级菜单,请更换！',
          type: 'warning',
          duration: 1500,
        })
        return;
      }
      if (data._level == 2) {
        this.placeholderUri = "请输入页面地址，如:system/setting/index";
      }
      if (data._level == 1) {
        this.placeholderUri = "请输入菜单简称，如：auth";
      }
      this.parentLevel = data._level;
      this.dataForm.parentId = data.id
      this.dataForm.parentName = data.name
    },
    // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode() {
      this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId)
      this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name']
    },
    // 图标选中
    iconActiveHandle(iconName) {
      this.dataForm.icon = iconName
    },
    // 表单提交
    dataFormSubmit() {
      this.buttonLoading = true;
      if (this.dataForm.type == 1 && this.dataForm.parentId == 0) {
        this.$notify({
          message: '请更换上级菜单',
          type: 'success',
          duration: 1500,
        })
        this.buttonLoading = false;
        return;
      }
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.id) {
            addMenu(this.dataForm).then((response) => {
              this.$notify({
                message: '新增成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.buttonLoading = false;
                  this.$emit('refreshDataList')
                }
              })
            }).catch(() => {
              this.buttonLoading = false;
            })
          } else {
            updateMenu(this.dataForm).then((response) => {
              this.$notify({
                message: '修改成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.buttonLoading = false;
                  this.$emit('refreshDataList');
                }
              })
            }).catch(() => {
              this.buttonLoading = false;
            })
          }
        } else {
          this.buttonLoading = false;
        }
      })
    }
  }
}
</script>

<style lang="scss">
.el-popper[x-placement^="top"] {
  overflow-y: scroll;
  min-width: 200px;
}
.mod-menu {
  .menu-list__input,
  .icon-list__input {
    > .el-input__inner {
      cursor: pointer;
    }
  }
  &__icon-popover {
    max-width: 27%;
  }
  &__icon-list {
    max-height: 250px;
    padding: 0;
    margin: -8px 0 0 -8px;
    > .el-button {
      padding: 8px;
      margin: 8px 0 0 8px;
      > span {
        display: inline-block;
        vertical-align: middle;
        width: 18px;
        height: 18px;
        font-size: 18px;
      }
    }
  }
  .icon-list__tips {
    font-size: 18px;
    text-align: center;
    color: #e6a23c;
    cursor: pointer;
  }
}
</style>
